package com.hex.ds.hdrs.app.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.hex.bigdata.hbda.base.Page;
import com.hex.bigdata.hbda.service.UserService;
import com.hex.bigdata.hbda.utils.SecurityUtils;
import com.hex.ds.hdrs.app.dao.GroupConfDao;
import com.hex.ds.hdrs.app.po.AppGroupRelationPo;
import com.hex.ds.hdrs.app.po.GroupConf;
import com.hex.ds.hdrs.app.service.IAppGroupRelationService;
import com.hex.ds.hdrs.common.constant.ErrorCode;
import com.hex.ds.hdrs.common.exception.BizException;
import com.hex.ds.hdrs.common.utils.BaseUtil;
import com.hex.ds.hdrs.common.utils.HdrsDateUtil;
import com.hex.ds.hdrs.common.utils.MapUtil;
import com.hex.ds.hdrs.engine.service.DispatcherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 分发器应用组 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2020-11-26
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class GroupConfService {

    @Autowired
    private GroupConfDao groupConfDao;

    @Autowired
    private IAppGroupRelationService appGroupRelationService;

    @Autowired
    private UserService userService;

    @Autowired
    private DispatcherService dispatcherService;


    /**
     * 分页查询
     *
     * @param groupConf
     * @param page
     * @return
     */
    public List<GroupConf> selectPages(GroupConf groupConf, Page page) {
        log.info("【进入AppGroupService的selectPages】");
        Map map = MapUtil.convertObjToMap(groupConf);
        return groupConfDao.selectPages(map, page);
    }


    public GroupConf qryGroupCodeByGroupCode(String groupCode) {
        log.info("【进入AppGroupService的qryGroupCodeByGroupCode】参数：{}", groupCode);
        return groupConfDao.qryGroupCodeByGroupCode(groupCode);
    }

    /**
     * 新增
     *
     * @param groupConf
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void add(GroupConf groupConf) {
        log.info("【AppGroupService.add】参数GroupConf: {}", groupConf.toString());
        groupConf.setPkId(BaseUtil.getPkId());
        groupConf.setCrtTs(HdrsDateUtil.getCurrentTime());
        groupConf.setUpTs(HdrsDateUtil.getCurrentTime());
        groupConf.setUpUser(SecurityUtils.getUsername());
        groupConfDao.add(groupConf);
        dispatcherService.addRefreshGroup(groupConf.getGroupCode(), groupConf.getThreadNum());
    }

    /**
     * 删除
     *
     * @param pkIds
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void del(List<String> pkIds) {
        log.info("【AppGroupService.del】参数List<GroupConf> groupConf】【{}】", pkIds.toString());
        if (CollectionUtil.isEmpty(pkIds)) {
            throw new BizException(ErrorCode.ERROR_910001);
        }
        if (!CollectionUtil.isEmpty(pkIds)) {
            for (String pkId : pkIds) {
                GroupConf groupConf = groupConfDao.qryGroupCodeByPkId(pkId);
                List<AppGroupRelationPo> appGroupRelationPos = appGroupRelationService.queryListByGroupCode(groupConf.getGroupCode());
                if (!CollectionUtil.isEmpty(appGroupRelationPos)) {
                    throw new BizException("910001", "应用组【" + groupConf.getGroupName() + "】在关联表中存在，无法删除");
                } else {
                    this.groupConfDao.del(pkId);
                }
            }
        }
    }

    /**
     * 修改
     *
     * @param groupConf
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void edit(GroupConf groupConf) {
        log.info("【AppGroupService.edit】参数GroupConf: {}", groupConf.toString());
        groupConf.setUpTs(HdrsDateUtil.getCurrentTime());
        groupConf.setUpUser(SecurityUtils.getUsername());
        System.out.println(groupConf.getUpUser());
        this.groupConfDao.edit(groupConf);
    }

    /**
     * 查重
     *
     * @param groupConf
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean checkAppGroupData(GroupConf groupConf) {
        log.info("【AppGroupService.checkAppGroupData】参数GroupConf: {}", groupConf.toString());
        Map map = MapUtil.convertObjToMap(groupConf);
        GroupConf dbGroupConf = this.groupConfDao.checkAppGroupData(map);
        if (dbGroupConf != null && !dbGroupConf.getPkId().equals(groupConf.getPkId())) {
            return false;
        }
        return true;
    }

    /**
     * 查询分组编码
     *
     * @return
     */
    public List<GroupConf> qryGroupCode() {
        log.debug("【进入了GroupConfService的qryGroupCode】");
        return groupConfDao.selectAllGroupConf();
    }
}
